﻿using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO.BusinessObjectRules
{
    public class DMClassAllSubClassesDiscriminatorsAreEqual : IBusinessObjectRule
    {
        public DMClass SuperClass{ private set; get;}
        public DMClass SubClass { private set; get; }

        public DMClassAllSubClassesDiscriminatorsAreEqual()
        {
//            if (dmClass == null) throw new ArgumentNullException("dmClass");
//            SuperClass = dmClass;
        }

        #region IBusinessObjectRule Members

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            SuperClass = (DMClass) bo;
            BusinessObjectCollection<DMInheritanceRelationship> subClassRelationships = SuperClass.SubClassRelationships;
            string value = "";
            foreach (DMInheritanceRelationship dmInheritanceRelationship in subClassRelationships)
            {
                if (String.IsNullOrEmpty(value))
                    value = dmInheritanceRelationship.Discriminator;
                else if (dmInheritanceRelationship.Discriminator != value)
                {
                    SubClass = dmInheritanceRelationship.SubClass;
                    return false;
                }
            }
            return true;
        }

        public string Name
        {
            get { return "AllSubClassesDiscriminatorsAreEqual"; }
        }

        public string Message
        {
            get { return "The SubClass '" + SubClass + "'s discriminator is not the same as the other SubClasses of the Entity/Class '" + SuperClass + "'."; }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Error; }
        }

        #endregion
    }
}
